package cn.xz.mxxz.activity

import android.os.Handler
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.LocalTAdapter
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.bean.AreaList
import cn.xz.mxxz.bean.CommonLib
import cn.xz.mxxz.bean.DeviceList
import cn.xz.mxxz.databinding.ActivityLocalBinding
import cn.xz.mxxz.dialog.LocalListDialog
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.util.*

/**
 * 通用客户端界面(本地播放器)*/
class LocalActivity : BaseActivity<ActivityLocalBinding>(R.layout.activity_local) {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //当前视频进度值
    private var pMove = 0
    private var handler: Handler? = null
    //当前多个设备的集合
    private var areaList: IntArray = intArrayOf(13186, 13185,13183)

    override fun initData() {
        //获取当前设备信息
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            if (CommonLib.MY_LOCAL_IP.isNotEmpty() && mDevice != null){
                mDevice!!.ip = CommonLib.MY_LOCAL_IP
            }
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            //设置标题
            mDataBind.tvTitle.text = ControlDevice.getDeviceName(mTag)
            //单独处理
            initOline()
            //设置分类
            setArea()
            //获取当前音量
            getVoice()
            //开启定时器获取视频进度
            handler = Handler()
            handler?.post(movieR)
        }
    }

    private fun initOline(){
        if (mTag == 14013){
            mDataBind.zkH.visible()
            mDataBind.zkK.visible()
        }
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //播放
        mDataBind.zkPlay.setOnClickListener {
            setPlay(1)
        }
        //暂停
        mDataBind.zkPause.setOnClickListener {
            setPlay(2)
        }
        //重播
        mDataBind.zkReplay.setOnClickListener {
            setPlay(3)
        }
        //最大化
        mDataBind.tvShow.setOnClickListener {
            setPlay(12)
        }
        //最小化
        mDataBind.tvHide.setOnClickListener {
            setPlay(11)
        }
        //列表
        mDataBind.zkList.setOnClickListener {
            getLocalList()
        }
        //上一页
        mDataBind.zkPageUp.setOnClickListener {
            setPlay(9)
        }
        //下一页
        mDataBind.zkPageDown.setOnClickListener {
            setPlay(10)
        }
        //鼠键
        mDataBind.zkSj.setOnClickListener {
            ARouter.to(SJActivity::class.java,mDevice!!.ip)
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, false)
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, true)
                pMove = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
                LocalHttp.changeMovie(mDevice!!.ip,pMove)
            }
        })
        mDataBind.zkH.setOnClickListener {
            ToastUtil.show("关屏幕")
        }
        mDataBind.zkK.setOnClickListener {
            ToastUtil.show("开屏幕")
        }
    }

    /*
     * @param ip
     * @param port
     * @param type
     * 1:播放
     * 2:暂停
     * 3:重播
     * 5:上一个
     * 6:下一个
     * 7：上一页
     * 8：下一页
     * 9：列表上一个
     * 10：列表下一个
     * 11：隐藏客户端
     * 12：显示客户端
     */
    private fun setPlay(i :Int){
        LocalHttp.moviePlay(mDevice!!.ip,i)
    }

    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip,LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
            }
        })
    }

    private fun getMove(){
        LocalHttp.getMovieSize(mDevice!!.ip,LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
            mDataBind.zkVideoSeekbar.post {
                mDataBind.zkMaxTime.text = maxTxt
                mDataBind.zkMinTime.text = minTxt
                mDataBind.zkVideoSeekbar.max = maxSeekBar
                mDataBind.zkVideoSeekbar.progress = seekProgress
            }
        })
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    //获取本地资源列表
    private fun getLocalList(){
        LocalHttp.getPlayData(this,mDevice!!.ip,LocalHttp.GetPlayDataListener {
            if (it.isNotEmpty()){
                LocalListDialog.newInstance(
                    mDevice!!,
                    it
                ).show(this@LocalActivity.supportFragmentManager,"LocalListDialog")
            }
        })
    }


    private fun setArea(){
        if (areaList.isNotEmpty()){
            val mL: ArrayList<AreaList> = arrayListOf()
            for (i in areaList.indices){
                val mD = ControlDevice.getThisDevice(areaList[i])
                if (mD != null){
                    mL.add(AreaList(id = areaList[i],name = mD.name, select = i ==0))
                }
            }
            val ms = LinearLayoutManager(this)
            ms.orientation = LinearLayoutManager.HORIZONTAL
            mDataBind.zkAreaRecycler.layoutManager = ms
            val mAdapter = LocalTAdapter()
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val data = adapter.data as ArrayList<AreaList>
                mDevice = ControlDevice.getThisDevice(data[position].id!!)
                data.forEach {
                    it.select = false
                    if (it.id == data[position].id){
                        it.select = true
                    }
                }
                adapter.notifyDataSetChanged()
            }
            mDataBind.zkAreaRecycler.adapter = mAdapter
            mAdapter.setList(mL)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }

}